package {
	import cn.flashk.controls.Button;
	import cn.flashk.controls.DataGrid;
	import cn.flashk.controls.Label;
	import cn.flashk.controls.TabBar;
	import cn.flashk.controls.TextArea;
	import cn.flashk.controls.TextInput;
	import cn.flashk.controls.managers.SkinLoader;
	import cn.flashk.controls.managers.SkinManager;
	import cn.flashk.ui.UI;
	
	import com._public.ObjectUtil;
	import com.flashdynamix.utils.SWFProfiler;
	import com.flashdynamix.utils.Util;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.net.LocalConnection;
	import flash.text.TextFormat;
	
	[SWF(frameRate="30",width="1000",height="600",backgroundColor="0xffffff")]
	public class Cookies extends Sprite {
		private var conn:LocalConnection;
		private var button:Button;
		private var format:TextFormat;
		private var format1:TextFormat;
		private var tab:TabBar;
		private var view:Sprite = new Sprite;
		private var monto:Sprite = new Sprite;
		private var serverData:DataGrid;
		private var localData:DataGrid;
		private var inputId:TextInput;
		private var inputState:TextInput;
		private var inputData:TextArea;
		private var inputName:TextInput;
		
		public function Cookies() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			SWFProfiler.init(stage, this);
			SkinManager.changeActionSkin(7);
			UI.init(stage)		//舞台
			SkinLoader.loadSkinFile("skin/assert/skin.swf");
			SkinLoader.eventDispatcher.addEventListener(SkinLoader.SKIN_LOADED,init)
			//init();
		}
		private function init(event:Event=null):void{
			button = new Button();
			addChild(button);
			button.label = "打开";
			button.moveTo(6,2);
			button.addEventListener(MouseEvent.CLICK,addLocalData);
			
			inputName = new TextInput();
			addChild(inputName);
			inputName.moveTo(button.width + 10,2);
			inputName.addEventListener(FocusEvent.FOCUS_OUT,changeHandler);
			
			tab = new TabBar();
			addChild(tab);
			tab.align = "right";
			tab.padding = 20; 
			tab.setSize(1000,600)
			tab.addTab("缓存查看",view);
			tab.addTab("缓存监控",monto);
			tab.switchTab(1);
			tab.y = 10;
			
			format = new TextFormat("微软雅黑",14,0x0,true);
			format1 = new TextFormat("微软雅黑",13,0x0);
			
			var label:Label = new Label();
			monto.addChild(label);
			label.text = "服务端数据监控";
			label.textFormat = format
			serverData = new DataGrid();
			monto.addChild(serverData);
			serverData.setSize(320,540);
			serverData.columnWidths = [-25.5,-30,-44]
			serverData.moveTo(0,label.height + 4);
			serverData.labels = ["ID","状态","时间"]
			serverData.dataField = ["id","type","time"];
			serverData.addEventListener(Event.CHANGE,handler);
			
			var btn0:Button = new Button();
			btn0.moveTo(label.x + serverData.width - btn0.width,label.y);
			btn0.name = "btn0";
			btn0.label = "清除";
			monto.addChild(btn0);
			btn0.addEventListener(MouseEvent.CLICK,clearLocalData);
			
			var label2:Label = new Label();
			monto.addChild(label2);
			label2.x = serverData.width + 10;
			label2.text = "本地端数据监控";
			label2.textFormat = format
			localData = new DataGrid();
			monto.addChild(localData);
			localData.setSize(320,540);
			localData.columnWidths = [-25.5,-30,-44]
			localData.moveTo(serverData.width + 10,label2.height + 4);
			localData.labels = ["ID","状态","时间"]
			localData.dataField = ["id","type","time"];
			localData.addEventListener(Event.CHANGE,handler);
			var btn:Button = new Button();
			btn.moveTo(label2.x + localData.width - btn.width,label2.y);
			btn.name = "btn";
			btn.label = "清除";
			monto.addChild(btn);
			btn.addEventListener(MouseEvent.CLICK,clearLocalData);
			
			/*for(var i:int=0;i<10;i++) {
				var item:Object = {};
				item.id = i+1;
				item.type = "type"+i;
				item.time = Util.getTime(new Date().getTime());
				localData.addItem(item);
				serverData.addItem(item);
			}*/
			
			var label3:Label = new Label();
			monto.addChild(label3);
			label3.x = localData.width + localData.x + 10;
			label3.text = "数据监控";
			label3.textFormat = format;
			
			var sprite:Sprite = new Sprite;
			monto.addChild(sprite);
			sprite.graphics.lineStyle(1,0x888B94);
			sprite.graphics.beginFill(0xffffff,0);
			sprite.graphics.drawRect(0,0,310,540);
			sprite.x = label3.x;
			sprite.y = label3.height + 4;
			
			var ID:Label = new Label();
			sprite.addChild(ID);
			ID.textFormat = format1;
			ID.text = "ID：";
			ID.moveTo(20,20);
			inputId = new TextInput();
			//inputId.textFormat = format1;
			sprite.addChild(inputId);
			inputId.moveTo(80,ID.y);
			
			var statelab:Label = new Label();
			sprite.addChild(statelab);
			statelab.textFormat = format1;
			statelab.text = "状态：";
			statelab.moveTo(20,60);
			inputState = new TextInput();
			sprite.addChild(inputState);
			inputState.moveTo(80,statelab.y);
			
			var data:Label = new Label();
			sprite.addChild(data);
			data.textFormat = format1;
			data.text = "数据：";
			data.moveTo(20,100);
			inputData = new TextArea();
			sprite.addChild(inputData);
			inputData.moveTo(80,data.y);
			inputData.setSize(216,384);
			
			connectData();
		}
		
		/**
		 * 清除数据
		 * @param event
		 */		
		protected function clearLocalData(event:MouseEvent):void {
			switch(event.currentTarget.name){
				case "btn0":
					if(serverData.length > 0){
						serverData.removeAll();
					}
					break;
				case "btn":
					if(localData.length > 0){
						localData.removeAll();
					}
					break;
			}
		}
		
		private function connectData():void {
			conn = new LocalConnection();
			conn.client = this;
			try {   
				conn.connect("LocalConnectionTest");   
			} catch (error:ArgumentError) {   
			}
		}
		
		protected function changeHandler(event:Event):void {
			trace(inputName.text)
			try {   
				conn.connect(inputName.text);   
			} catch (error:ArgumentError) {   
			}
		}
		
		public function addLocalData(e:MouseEvent):void{
			var item:Object = {};
			item.id 	= "id";
			item.type 	= "type";
			item.data 	= "data";
			item.time  	= Util.getSysDate(new Date().getTime());
			serverData.addItem(item);
		}
		
		public function receive(type:String,id:String,data:Object):void{
			var item:Object = {};
			item.id 	= id;
			item.type 	= type;
			item.data 	= ObjectUtil.toString(data);
			item.time  	= Util.getSysDate(new Date().getTime());
			if(type=="in"){
				serverData.addItemAt(item,0);
			}else if(type=="out"){
				localData.addItemAt(item,0);
			}
		}
		
		private function handler(e:Event):void {
			var dg:DataGrid = e.currentTarget as DataGrid;
			inputId.text    = dg.selectedItem.id;
			inputState.text = dg.selectedItem.type;
			inputData.text  = dg.selectedItem.data;
		}
	}
}